import { readonly, ref } from 'vue';
import { GameKernel, EVENTS } from '../game';
import {
  FEATURE_UNLOCK_ID,
  type FeatureId,
} from '../game/data/feature-unlock/type';

const kernel = GameKernel.inst;

const state = ref(kernel.cultivationModel.uiState);

kernel.bus.on(EVENTS.CULTIVATION_STATE_UPDATE, () => {
  state.value = kernel.cultivationModel.uiState;
});
kernel.bus.on(EVENTS.GAME_LOADED, () => {
  state.value = kernel.cultivationModel.uiState;
});

const menuList = readonly([
  {
    id: FEATURE_UNLOCK_ID.CULTIVATION_SKILL,
    label: '功法',
  },
  {
    id: FEATURE_UNLOCK_ID.SPIRIT_ROOT,
    label: '灵根',
  },
]);

const activeMenu = ref<FeatureId>(FEATURE_UNLOCK_ID.CULTIVATION);

export function useCultivation() {
  state.value = kernel.cultivationModel.uiState;

  function changeMenu(id: FeatureId) {
    activeMenu.value = id;
  }

  function back() {
    activeMenu.value = FEATURE_UNLOCK_ID.CULTIVATION;
  }

  return { state, menuList, activeMenu, changeMenu, back };
}
